<?php
session_start();
include_once '../../model/user/user_login.php';
include_once '../../model/user/admin_create.php';
$name = $_POST['username'];
$pass = md5($_POST['password']);
$check_user = new UserLogin();
$level = mysql_fetch_array($check_user->check_level($name));
if (isset($_POST['save_user']) && isset($_POST["submit"])) {
    if ($check_user->check_login($name, $pass) > 0) {
        if ($level != '') {
            $check_user->set_session('id', $level["user_id"]);
            $check_user->set_session('username', $name);
            $check_user->set_session('level', $level["level"]);
            $check_user->set_cookie('username', $name, time() * 3600);
            $check_user->set_cookie('level', $level, time() * 3600);
        }
    } else {
        header("location:../../login");
    }
// Tạo tài khoản admin
} else if (isset($_POST['create'])) {
    $admin = new admin();
    if ($admin->check_user($name) == 0) {
        $insert = $admin->InsertAdmin($_POST);
        if ($insert) {
            $check_user->set_session('id', $level["user_id"]);
            $check_user->set_session('username', $name);
            $check_user->set_session('level', $level["level"]);
            header("location:../../index");
        } else {
            header("location:../../login/err-insert");
        }
    } else {
        header("location:../../login/err-create");
    }
} else if (isset($_POST["submit"])) {
    if ($check_user->check_login($name, $pass) > 0) {
        if ($level != '') {
            $check_user->set_session('id', $level["user_id"]);
            $check_user->set_session('username', $name);
            $check_user->set_session('level', $level["level"]);
            header("location:../../index");
        }
    } else {
        header("location:../../login/error");
    }
}
?>